package org.aner.tester.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * aner-tester configuration
 *
 * @author aner
 * @version 1.0
 * @date 2020/12/11 14:52
 */
public class TesterConfig {

    private static Logger logger = LoggerFactory.getLogger(TesterConfig.class);

    private static String profilePath= "/aner-tester/anertester-config.yml";

    private static Map yamlMap = new HashMap<>();

    /**
     * form data type
     */
    private String contentTypeFormData;

    /**
     * payload data type
     */
    private String contentTypePayload;

    /**
     * token identification
     */
    private String header;

    /**
     *token value
     */
    private String testToken;

    /**
     * whether authorization info is required where the api are be tested
     */
    private boolean login;

    public void init() {

        logger.info("Reading aner-tester configuration file...");
        readYaml();
        Map<String, Object> anerTesterConfigMap = (Map<String, Object>)yamlMap.get("aner-tester");
        Map<String, String> contentTypeMap = (Map<String, String>)anerTesterConfigMap.get("content-type");
        contentTypeFormData = contentTypeMap.get("form-data");
        contentTypePayload = contentTypeMap.get("payload");

        Map<String, Object> loginInfoMap = (Map<String, Object>)anerTesterConfigMap.get("login-info");
        header = (String)loginInfoMap.get("header");
        testToken = (String)loginInfoMap.get("test-token");
        login = (boolean)loginInfoMap.get("is-login") ;
    }

    private void readYaml() {
        try{
            Yaml yaml=new Yaml();
            InputStream stream = TesterConfig.class.getResourceAsStream(profilePath);
            yamlMap = (HashMap<String,Map>) yaml.load(stream);
        }catch (Exception e){

            throw new RuntimeException("read yaml file failure");
        }
    }

    public String getContentTypeFormData() {
        return contentTypeFormData;
    }

    public void setContentTypeFormData(String contentTypeFormData) {
        this.contentTypeFormData = contentTypeFormData;
    }

    public String getContentTypePayload() {
        return contentTypePayload;
    }

    public void setContentTypePayload(String contentTypePayload) {
        this.contentTypePayload = contentTypePayload;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        header = header;
    }

    public String getTestToken() {
        return testToken;
    }

    public void setTestToken(String testToken) {
        testToken = testToken;
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean isLogin) {
        isLogin = isLogin;
    }

    private TesterConfig() {}

    public static TesterConfig me() {
        return AnerTesterConfigHolder.INSTANCE;
    }

    private static class AnerTesterConfigHolder {
        public static final TesterConfig INSTANCE = new TesterConfig();
    }

}
